package com.pz.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.pz.gulimall.product.entity.AttrEntity;
import com.pz.gulimall.product.service.AttrAttrgroupRelationService;
import com.pz.gulimall.product.service.AttrService;
import com.pz.gulimall.product.service.impl.AttrGroupServiceImpl;
import com.pz.gulimall.product.vo.req.AttrGroupRelationReqVo;
import com.pz.gulimall.product.vo.res.AttrGroupWithAttrsResVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.pz.gulimall.product.entity.AttrGroupEntity;
import com.pz.gulimall.product.service.AttrGroupService;
import com.pz.gulimall.common.utils.PageUtils;
import com.pz.gulimall.common.utils.R;

import javax.annotation.Resource;


/**
 * 属性分组
 *
 * @author PZ
 * @email pzs@gmail.com
 * @date 2024-02-01 20:22:28
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private AttrGroupServiceImpl attrGroupServiceImpl;
    @Resource
    private AttrService attrService;
    @Resource
    private AttrAttrgroupRelationService attrGroupRelationService;


    /**
     * 获取 当前分类下 所有分组 和 关联属性
     * @param catelogId
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getBrandsAndGroupByCatId(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsResVo> list = attrGroupService.getAttrGroupWithAttrByCatId(catelogId);
        return R.ok().put("data", list);
    }

    /**
     * 列表
     * 规定 catelogId 为 0 则列出所有
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable(value = "catelogId") Long categoryId){
        PageUtils page = attrGroupServiceImpl.queryPage(params, categoryId);
        return R.ok().put("page", page);

    }

    @GetMapping("/{attrGroupId}/attr/relation")
    public R listRelation(@PathVariable("attrGroupId")Long attrGroupId){
        List<AttrEntity> list = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data", list);
    }

    @PostMapping("/attr/relation/delete")
    public R delRelation(@RequestBody AttrGroupRelationReqVo[] vos){
        attrService.delRelation(vos);
        return R.ok();
    }

    /**
     * 获取 attrGroupId 没有关联到的 attr
     * @param attrGroupId
     * @return
     */
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R getNoRelationAttr(@PathVariable("attrGroupId")Long attrGroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoRelationAttr(params, attrGroupId);
        return R.ok().put("page", page);
    }

    @PostMapping("/attr/relation")
    public R addAttrGroupRelation(@RequestBody List<AttrGroupRelationReqVo> reqVo){
        attrGroupRelationService.saveRelation(reqVo);
        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupServiceImpl.getAttrAndCatePathById(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
